#!/bin/bash

if [[ -z "$1" ]]; then echo "Error: Please specify the language to test"; exit 1; fi

LANG="$1"

if [[ ! -d "lang/$LANG" ]]; then echo "Error: Language $LANG not found in lang/ directory";             exit 1; fi

if [[ ! -f "lang/$LANG/compile" ]]; then echo "Error: Compilation script lang/$LANG/compile does not exist";    exit 1; fi
if [[ ! -x "lang/$LANG/compile" ]]; then echo "Error: Compilation script lang/$LANG/compile is not executable"; exit 1; fi

if [[ ! -f "lang/$LANG/run" ]]; then echo "Error: Run script lang/$LANG/run does not exist";    exit 1; fi
if [[ ! -x "lang/$LANG/run" ]]; then echo "Error: Run script lang/$LANG/run is not executable"; exit 1; fi

if [[ ! -f "lang/$LANG/extension" ]]; then echo "Error: Extension file lang/$LANG/extension does not exist";    exit 1; fi

#relative to tmp

COMPILE="../lang/$LANG/compile"
RUN="../lang/$LANG/run"
EXT=`cat "lang/$LANG/extension"`

mkdir tmp
cd tmp

for PROBLEM in ../problems/* ../problems/\!$EXT/*
do
    rm -rf *
    PROBNAME="${PROBLEM##*/}"
    if [[ -d "$PROBLEM" && ! "${PROBNAME:0:1}" == "!" ]]
    then
        echo "Test problem: $PROBNAME"

        SHOULD_FAIL="false"
        if [[ "$PROBNAME" == *-fail ]]
        then
            SHOULD_FAIL="true"
            PROBNAME="${PROBNAME:0:${#PROBNAME}-5}"
        fi
        if [[ ! -f "$PROBLEM/$PROBNAME.$EXT" ]]
        then echo "[$PROBNAME] Warning: file $PROBLEM/$PROBNAME.$EXT is not found. Skipping problem \"$PROBNAME\""
        else
            for SRC in $PROBLEM/*.$EXT
            do
                if [[ ! "$SRC" == */Tests.scala ]]
                then
                    echo "[$PROBNAME] Copying source file $SRC..." && cp "$SRC" .
                fi
            done
            echo "[$PROBNAME] Compiling $PROBNAME.$EXT..."
            if $COMPILE $PROBNAME.$EXT
            then
                if [[ "$SHOULD_FAIL" == "true" ]]
                then
                    echo "[$PROBNAME] FAIL: Compilation succeeded unexpectedly."
                else
                    echo "[$PROBNAME] Compilation succeeded as expected."
                    if [[ -f "$PROBLEM/Tests.scala" ]]
                    then
                        echo "[$PROBNAME] Running the compiled program on tests..."
                        echo -n "[$PROBNAME] Generating tests..."
                        cp "$PROBLEM/Tests.scala" .
                        ../lang/scala/compile Tests.scala
                        ../lang/scala/run Tests
                        rm -f Tests*
                        echo " done."
                        VERDICT="OK"
                        for TST in tests/??
                        do
                            echo -n "[$PROBNAME] Running on test ${TST:${#TST}-2}... "
                            if ( $RUN $PROBNAME < $TST > output 2> time.out )
                            then
                                if [[ `diff -w $TST.a output` == "" ]]
                                then
                                    echo "OK"
                                else
                                    echo "Wrong answer"
                                    VERDICT="FAIL"
                                fi
                            else
                                if [[ `grep "signal 9" time.out` != "" ]]
                                then
                                    echo "Time limit of 10 seconds exceeded"
                                else
                                    echo "Runtime error. Exit code: $?"
                                    cat time.out
                                fi
                                VERDICT="FAIL"
                            fi
                        done
                        echo "[$PROBNAME] $VERDICT"
                    else
                        echo "[$PROBNAME] Running the compiled program..."
                        if ( ulimit -t 10 && $RUN $PROBNAME > output 2> time.out )
                        then
                            echo "[$PROBNAME] Program terminated successfully."
                            if [[ -f "$PROBLEM/answer" ]]
                            then
                                if [[ `diff -w "$PROBLEM/answer" output` == "" ]]
                                then
                                    echo "[$PROBNAME] OK"
                                else
                                    echo "[$PROBNAME] FAIL: WRONG ANSWER"
                                fi
                            else
                                echo "[$PROBNAME] OK"
                            fi
                        else
                            if [[ `grep "signal 9" time.out` != "" ]]
                            then
                                echo "[$PROBNAME] FAIL: Time limit of 10 seconds exceeded"
                            else
                                echo "[$PROBNAME] FAIL: Runtime error. Exit code: $?"
                            fi
                        fi
                    fi
                fi
            else
                if [[ "$SHOULD_FAIL" == "true" ]]
                then
                    echo "[$PROBNAME] OK: Compilation failed as expected."
                else
                    echo "[$PROBNAME] FAIL: Compilation failed unexpectedly."
                fi
            fi
        fi
        echo ""
    fi
done

cd ..
rm -rf tmp
